package com.tianzmp.ttq.admin.service.impl;
import com.tianzmp.ttq.admin.util.ResourceUtils;
import org.krysalis.barcode4j.HumanReadablePlacement;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.DataMatrixWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.io.FileUtils;
import org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean;
import org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class QrCode {

    private static final String CHARSET = "utf-8";
    private static final String FORMAT_NAME = "PNG";

    private static final int QRCODE_SIZE = 300;

    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;

    public static void main(String[] args) throws Exception{
        //6400010157
//        byte[] bytes = "你好".getBytes(StandardCharsets.US_ASCII);
//        String s = new String(bytes, StandardCharsets.US_ASCII);
//        BufferedImage qrCode1 = QrCode.createQrCode1(s, "D://Desktop//test2.png");
//
//        ImageIO.write(qrCode1,"png",new File("D://Desktop//test2.png"));

//        QrCode.createQrCode("6400010157", "D://Desktop//test3.png");

        //QrCode.genQRCode("6400010157", "D://Desktop//test3.png");

        QrCode.createQrCode("6400010157", "D://Desktop//test3.png",62,62);

    }


    private static void createQrCode(String content,String imagePath,int width,int height) throws Exception {
        if ("".equals(content) || null == content){
            System.out.println("内容异常");
            return;
        }
        if ("".equals(imagePath) || null == imagePath){
            System.out.println("文件路径异常");
            return;
        }
        Map<EncodeHintType, Object> hints = new ConcurrentHashMap<>(3);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET,CHARSET);
        hints.put(EncodeHintType.MARGIN,0);

        BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.DATA_MATRIX, width, height,hints);
        BufferedImage bufferedImage = new BufferedImage(encode.getWidth(), encode.getHeight(), BufferedImage.TYPE_INT_BGR);
        for (int i = 1; i< width; i++){
            for (int j= 1; j <height; j++){
                bufferedImage.setRGB(i,j,encode.get(i,j) ? 0xFF000000 : 0xFFFFFFF);
            }
        }
        ImageIO.write(bufferedImage,FORMAT_NAME,new File(imagePath));
    }

    public static BufferedImage createQrCode1(String content,String imagePath) throws IOException {
        if ("".equals(content) || null == content){
            System.out.println("内容异常");
            return null;
        }
        if ("".equals(imagePath) || null == imagePath){
            System.out.println("文件路径异常");
            return null;
        }
        Map<EncodeHintType, Object> hints = new ConcurrentHashMap<>(3);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET,CHARSET);
        hints.put(EncodeHintType.MARGIN,0);
        DataMatrixWriter dataMatrixWriter = new DataMatrixWriter();
        BitMatrix encode = dataMatrixWriter.encode(content, BarcodeFormat.DATA_MATRIX, WIDTH, HEIGHT);
        int width = encode.getWidth();
        int height = encode.getHeight();
        System.out.println(width+"---"+height);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics2D graphics = (Graphics2D)bufferedImage.getGraphics();
        graphics.setBackground(Color.white);
        graphics.clearRect(0,0,height,height);
        for (int i = 1; i< width; i++){
            for (int j= 1; j <height; j++){
                bufferedImage.setRGB(i,j,encode.get(i,j) ? 0xFF000000 : 0xFFFFFFF);
            }
        }
        return bufferedImage;
        //ImageIO.write(bufferedImage,FORMAT_NAME,new File(imagePath));
    }


    /**
    * <pre>
     *     获取dataMatrix码
    * </pre>
    * @param content
    * @param imagePath
    * @return void
    * @author zhump
    * @date 2023/10/25 13:59
    */
    public static void getDataMatrixCode(String content,String imagePath) throws IOException {
        if ("".equals(content) || null == content){
            System.out.println("内容异常");
            return;
        }
        if ("".equals(imagePath) || null == imagePath){
            System.out.println("文件路径异常");
            return;
        }
        DataMatrixWriter dataMatrixWriter = new DataMatrixWriter();
        BitMatrix encode = dataMatrixWriter.encode(content, BarcodeFormat.DATA_MATRIX, WIDTH, HEIGHT);
        int width = encode.getWidth();
        int height = encode.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics2D graphics = (Graphics2D)bufferedImage.getGraphics();
        //设置背景为白色
        graphics.setBackground(Color.white);
        //清除底色
        graphics.clearRect(0,0,height,height);
        for (int i = 1; i< width; i++){
            for (int j= 1; j <height; j++){
                bufferedImage.setRGB(i,j,encode.get(i,j) ? 0xFF000000 : 0xFFFFFFF);
            }
        }
        ImageIO.write(bufferedImage,FORMAT_NAME,new File(imagePath));
    }


    /**
     *
     * @param message   内容
     * @param filePath  文件生成路径
     */
    public static void genQRCode(String message,String filePath){
        try {
//            BarcodeUtil util = BarcodeUtil.getInstance();
//            DefaultConfiguration cfg = new DefaultConfiguration("barcode");
//            DefaultConfiguration child = new DefaultConfiguration("datamatrix");
//            cfg.addChild(child);
//
//
//            DefaultConfiguration attr = new DefaultConfiguration("human-readable");
//
//            attr = new DefaultConfiguration("height");
//            attr.setValue(50);
//            child.addChild(attr);
//
//            attr = new DefaultConfiguration("module-width");
//            attr.setValue("2.0");
//            child.addChild(attr);
//
//            BarcodeGenerator gen = util.createBarcodeGenerator(cfg);
//            int resolution = 300;
//
//
//            String format = "image/png";
//            ByteArrayOutputStream ous = new ByteArrayOutputStream();
//            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//            gen.generateBarcode(canvas,message);
//
//
//            canvas.finish();
            //http://datamatrix.kaywa.com/
//            byte[] bytes = ous.toByteArray();
//            FileUtils.writeByteArrayToFile(new File(filePath),bytes);
            DataMatrixBean bean = new DataMatrixBean();
            bean.setShape(SymbolShapeHint.FORCE_SQUARE);
            bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
            bean.doQuietZone(false);


            final int dpi = 4096;
            String format = "image/png";
            ByteArrayOutputStream ous = new ByteArrayOutputStream();
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,BufferedImage.TYPE_BYTE_BINARY, false, 0);
            bean.generateBarcode(canvas, message);
            canvas.finish();
            byte[] bytes = ous.toByteArray();
            FileUtils.writeByteArrayToFile(new File(filePath),bytes);

            double verticalQuietZone = bean.getVerticalQuietZone();
            String pattern = bean.getPattern();
            String fontName = bean.getFontName();
            System.out.println(verticalQuietZone);
            System.out.println(pattern);
            System.out.println(fontName);


        }catch (Exception e){
            e.printStackTrace();
        }
    }












}
